package com.jd.jr.insights.dm.dim.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.jd.jr.insights.dm.dim.dao.DeptDao;

import com.jd.jr.insights.dm.dim.dao.impl.DeptDaoImpl;
import com.jd.jr.insights.dm.dim.domain.Department;
import com.jd.jr.insights.dm.dim.service.DeptService;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.Result;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.ResultCode;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.Tree;
import com.jd.jr.insights.dm.dim.util.fastjson.tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guoxuefei1 on 2017/7/18.
 */
public class DeptServiceImpl implements DeptService {
    DeptDao deptDao = new DeptDaoImpl();

    public String getDeptListJson() {
        Tree tree = new Tree(deptDao.getList());
        TreeNode treeNode = tree.getTreeNode("2");

        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); // 构造方法里，也可以直接传需要序列化的属性名字
        filter.getExcludes().add("parent");
        filter.getExcludes().add("allChildren");
        List<TreeNode> list=new ArrayList<TreeNode>();
        list.add(treeNode);
       return JSONObject.toJSONString(list, filter);
        //return JSON.toJSONString(deptDao.getList());
    }


    public String getDeptListJsonNm() {
        return JSON.toJSONString(deptDao.getList());
    }

    public String deleteById(Department dept) {
        if(deptDao.deleteById(dept)==true){
            return JSON.toJSONString(new Result(ResultCode.OK));
        }
        return JSON.toJSONString(new Result(ResultCode.ERROT));
    }

    public String save(Department dept) {
        if(deptDao.save(dept)==true){
            return JSON.toJSONString(new Result(ResultCode.OK));
        }
        return JSON.toJSONString(new Result(ResultCode.ERROT));
    }


    public String update(Department dept) {
        if(deptDao.update(dept)==true){
            return JSON.toJSONString(new Result(ResultCode.OK));
        }
        return JSON.toJSONString(new Result(ResultCode.ERROT));
    }


}
